from openpyxl import Workbook
from openpyxl.utils import get_column_letter
import requests
from bs4 import BeautifulSoup

# 示例HTML字符串
html_string = "<table><img src=\"https://img-s-msn-com.akamaized.net/tenant/amp/entityid/BB1oC6Pn.img\" height=50 width=50></table>"

img_url_0 = "https://support.content.office.net/en-us/media/2d9e717a-0077-438f-8e5e-f85a1305d4ad.jpg"
# 使用BeautifulSoup解析HTML
soup = BeautifulSoup(html_string, 'html.parser')
img_tag = soup.find('img')

if img_tag and 'src' in img_tag.attrs:
    img_url = img_tag['src']

    # 创建工作簿和工作表
    wb = Workbook()
    ws = wb.active
    ws.title = "Images"

    # 插入图片URL到单元格A1
    ws['A1'] = 1
    ws['B1'] = 2
    ws['C1'] = "=SUM(A1:B1)"
    ws['A2'] = img_url_0
    ws['B2'] = "=IMAGE(\"https://support.content.office.net/en-us/media/2d9e717a-0077-438f-8e5e-f85a1305d4ad.jpg\")"
    ws['A3'] = html_string
    # 保存工作簿
    wb.save("ImageURLInExcel.xlsx")

else:
    print("No valid <img> tag found in HTML.")
